class Request {
  baseUrl = "https://wechat.kaiwumace.com"; //  http://172.168.1.117:6070

  getData({
    url,
    method = 'GET',
    data = {}
  }) {
    const token = wx.getStorageSync('token')
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.baseUrl + url,
        method: method,
        header: {
          'content-type': 'application/json;charset=UTF-8',
          'Authorization': token
        },
        data: data,
        success: res => {
          if (res.data.code == 1) {
            resolve(res.data)
          } else {
            this.showError(res.data.desc)
            if (res.data.code == 2 || res.data.code == 10) {
              setTimeout(()=>{
                wx.clearStorageSync()
                wx.reLaunch({
                  url: '/pages/login/login'
                })
              },2000) 
            }
          }
        },
        fail: err => {
          reject(err)
          this.showError()
        }
      })
    })
  }

  showError(error) {
    if (error) {
      wx.hideLoading()
      wx.showToast({
        title: error,
        icon: "none"
      })
    } else {
      wx.hideLoading()
      wx.showToast({
        title: "请求错误",
        icon: "none"
      })
    }
  }
}

export {
  Request
}